<?PHP
class db
{
		  var $hostname = "localhost";
		  var $username = "someuser";
		  var $password = "a1b2c3d4e5";
		  var $database = "ChelleWeb";
		  var $error = "";
	 private function connect()
	 {	  
		  $connection = mysql_connect($this->hostname, $this->username, $this->password);
		  if(!$connection) {
		  	$this->error .= "Could not connect: " . mysql_error() . "<br />";
		} else {
			$this->error .= "No connection error <br />";
		}
		$dbselect = mysql_select_db($this->database, $connection);
		if(!$dbselect) {
			$this->error .= "Could not select DB: " . mysql_error() . "<br />";
		} else {
			$this->error .= "No dbselect error <br />";
		}
	 }
	 public function get_page($page)
	 {
	 		$this->error .= "GET_CONTENT: <br />";
	 		$this->connect();
			$p = $page;
			if($p == ""){
				$p = "&nbsp;";
			}
			$sql = "SELECT page,content FROM content WHERE page = '$p'";
			$result = mysql_query($sql);
			if(!$result) {
				$this->error .= " get_content() query failed: " .mysql_error() . "<br />";
			}else {
				$result = mysql_fetch_assoc($result);
				$contRAW = $result[content];
				$cont = html_entity_decode($contRAW, ENT_QUOTES);
				return $cont;
			}
	 }
	 
	 public function set_page($page, $cont)
	 /*
	 	page to change ($page) and content ($cont) is given to the function
		get_content is used to check if the page already exist.
		mysql_num_rows checks the amount of returned rows.
		IF above 0 the content in the DB is replaced with the $cont
		ELSE a new entry is created in the DB with $page and $cont
		The returned message in each case specifies what was done.
	 */
	 {
		$p = $page;
		$c = htmlentities($cont, ENT_QUOTES);
		$msg = "NOTHING HAPPEND";
		$test = $this->get_content($p);
		if($test) {
			$sql = "UPDATE content SET content='$c' WHERE page='$p'";
			$msg = $p . " already existed. Content was updated.";
		} else {
			$sql = "INSERT INTO content VALUES ('$p', '$c')";
			$msg = $p . " did not exist yet. It has been created.";
		}
		$this->error .= "SET_CONTENT: <br />";
		$this->connect();
		$result = mysql_query($sql);
		if(!$result) {
			$this->error .= "set_content() query failed: " .mysql_error() . "<br />";
		}	
		return $msg;
	 }
	public function error()
	 {
		return $this->error;
	 }
	 public function get_task($task, $group)
	 {
		$this->error .= "GET_TASK: <br />";
	 		$this->connect();
			$tid = $task;
			$gid = $group;
			$sql = "SELECT opgave,oplossing,oid FROM taak WHERE tid = '$tid'";
			$result = mysql_query($sql);
			if(!$result) {
				$this->error .= " get_task() query failed: " .mysql_error() . "<br />";
			}else {
				/*$result = mysql_fetch_assoc($result);*/
				return $result;
			}
	 }
	 public function set_task($k, $t, $opg, $opl)
	 {
		 $this->error .= "SET_TASK: <br />";
		 $klas = $this->get_gid($k);
		 if($klas = 0){
			 $this->error .= "get_gid() returned 0, creating klas <br />";
			 $sql = "INSERT INTO klas VALUES ('$klas')";
			 $result = mysql_query($sql);
			 if ($result){
				 $this->error .= "insert statement worked <br />";
				 $klas = $this->get_gid($k);
			 }else{
				 $this->error .= "insert statement failed " .mysql_error() . "<br />";
			 }
		 }
		 $taak = $this->get_tid($t);
		 if($taak = 0){
			$this->error .= "get_tid() returned 0, creating taak <br />";
			$sql = "INSERT INTO taken VALUES ('$klas','$t')";
			$msg = mysql_query($sql);
		 }else{
			 
		 }
		 return $msg;
	 }	
	 private function get_tid($name)
	 {
		$this->connect();
		$t = strtolower($name);
		$sql = "SELECT tid FROM taken WHERE tname = '$t'";
		$result = mysql_query($sql);
		$this->error .= "GET_TID: <br />";
		if(!$result) {
			$this->error .= "get_tid() tname to TID query failed: " .mysql_error() . "<br />";
			$tid = 0;
			}else {
				$result = mysql_fetch_assoc($result);
				$tid = $result[tid];
				$this->error .= "TID = " . $tid ."<br />";
			}
		 return $tid;
	 }
	 private function get_gid($name)
	 {
		 $this->connect();
		$g = strtolower($name);
		$sql = "SELECT gid FROM klas WHERE gname = '$g'";
		$result = mysql_query($sql);
		$this->error .= "GET_GID: <br />";
		$result = mysql_fetch_assoc($result);
			if(!$result) {
				$this->error .= "get_gid() gname to GID query failed: " .mysql_error() . "<br />";
				$gid = 0;
				$this->error .= "GID = " . $gid ."<br />";
			}else {
				$gid = $result[gid];
				$this->error .= "GID = " . $gid ."<br />";
			}
	 	return $gid;
	 }
	 public function set_klas($k)
	 {
		 $this->error .= "SET_KLAS(): <br />";
		 $klas = strtolower($k);
		 $klasID = $this->get_gid($klas);
	 	 if($klasID == 0){
			 $this->error .= "klas bestaat nog niet, wordt gemaakt <br />";
		 	 $sql = "INSERT INTO klas (gname) VALUES ('$klas')";
			 $this->connect();
			 $result = mysql_query($sql);
			 if(!result){
				 $this->error .= "INSERT klas failed " . mysql_error() . "<br />";
				 $msg = "error, check log";
			 }else{
				 $msg = "De klas met naam " . $klas . " is gemaakt";
			 }
		 }else{
			 $msg = "Sorry een klas met deze naam bestaat reeds";
		 }
		 return $msg;
	 }
	 public function get_klas()
	 {
		 $this->error .= "GET_KLAS(): <br />";
		 $sql = "SELECT * from klas";
		 $this->connect();
		 $result = mysql_query($sql);
		 if(!$result) {
			 $this->error .= "get_klas() error: " .mysql_error() . "<br />";
		 }else{
			 $rows = mysql_num_rows($result);
			 $this->error .= "get_klas(): sql success, number of rows is: " . $rows . "<br />";
		 }
		 return $result;
	 }
	 	 public function get_taken($k)
	 {
		 $klas = $k;
		 $this->error .= "GET_TAKEN(): <br />";
		 $sql = "SELECT * from taken WHERE gid = $k";
		 $this->connect();
		 $result = mysql_query($sql);
		 if(!$result) {
			 $this->error .= "get_taken() error: " .mysql_error() . "<br />";
		 }else{
			 $rows = mysql_num_rows($result);
			 $this->error .= "get_taken(): sql success, number of rows is: " . $rows . "<br />";
		 }
		 return $result;
	 }
	 public function del_klas($k)
	 {
		 $klasID = $k;
		 $this->error .= "DEL_KLAS(" . $klasID . "): <br />";
		 $sql = "select * from taken where gid = '$klasID' LIMIT 1";
		 $this->connect();
		 $result = mysql_query($sql);
		 if(!$result){
			 $this->error .= "select * from taken error: " .mysql_error() . "<br />";
		 }
		 $this->error .= "Select * from taken resultaat: " .$result . "<br />";
		 $num = mysql_num_rows($result);
		 $this->error .= "number of rows: " . $num . "<br />";
		 	if(mysql_num_rows($result) != 1) {
			 	$sql = "DELETE from klas WHERE gid = $klasID";
				$result = mysql_query($sql);
				if(!$result){
			 		$this->error .= "delete from klas where gid failed: " .mysql_error() . "<br />";
				 }
				$msg = "De klas met naam " . $klasID . " was verwijderd.";
			}else{
				$msg = "Verwijder eerst alle verbonden taken.";
		  	}
		 return $msg;
	 } 
}
